package com.projectfile.config;

import com.projectfile.service.CustomUserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class LoginSecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    @Bean
    UserDetailsService customUserService(){
        return new CustomUserService();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder authent) throws Exception {
        authent
                .userDetailsService(customUserService())
                .passwordEncoder(passwordEncoder());
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/login/**").permitAll()
                .successForwardUrl("/login/**").permitAll()
                .failureUrl("/login?error");
        http.authorizeRequests()
                .antMatchers("/login/**").permitAll()
                .antMatchers("/api/**").permitAll()
                .antMatchers("/api/**/**").permitAll()
                .antMatchers("/**/login/**").permitAll()
                .antMatchers("/save/**").permitAll()
                .antMatchers("/save").permitAll()
                .antMatchers("/save/").permitAll()
                .antMatchers("/**").permitAll();
                //.anyRequest().authenticated() ;//任何请求,登录后可以访问
        http.logout().permitAll();
        http.csrf().disable();


    }

}
